/*
 * Filename:    AuditDispatchController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-10
 */
package com.paic.mhis.hcpms.feedback.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.common.base.dto.PaginationDTO;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.mhis.hcpms.audit.biz.service.ICaseCheckService;
import com.paic.mhis.hcpms.audit.dto.AuditNotificationDTO;
import com.paic.mhis.hcpms.feedback.biz.service.AuditFeedbackService;
import com.paic.mhis.hcpms.feedback.exception.AuditFeedbackException;
import com.paic.mhis.hcpms.feedback.web.vo.AuditFeedbackVO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;

/**
 * 稽核医院反馈
 * 
 * @author EX-XIEQI001
 * 
 */
@Controller
@RequestMapping("/auditFeedback/")
public class AuditFeedbackController {

	@BizServicesProxy(beanName = "auditFeedbackService")
	private AuditFeedbackService auditFeedbackService;

	@BizServicesProxy(beanName = "caseCheckServcieImpl")
	private ICaseCheckService caseCheckService;

	@RequestMapping("getAuditNoticationInfo.do")
	@ResponseBody
	public Map<String, Object> getAuditNoticationInfo(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String caseCode = request.getParameter("caseCode");
		AuditNotificationDTO returnBean = this.caseCheckService.getAuditNotificationDTO(caseCode);

		map.put("result", returnBean);

		return map;
	}

	/**
	 * 获取所有的医院反馈数据
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("queryAuditFeedbackList.do")
	@ResponseBody
	public Map<String, Object> queryAuditFeedbackList(@ModelAttribute AuditFeedbackVO vo, HttpServletRequest request)
			throws AuditFeedbackException {
		PaginationDTO pdto = new PaginationDTO();
		if (null == vo) {
			vo = new AuditFeedbackVO();
			vo.setLimit(20);
		}
		if (null != vo && vo.getLimit() <= 0) {
			vo.setLimit(20);
		}
		int start = 0, end = 0;
		if (null != vo && vo.getPage() > 1) {
			start = (vo.getPage() - 1) * vo.getLimit();
			end = vo.getPage() * vo.getLimit();
		} else {
			start = 0;
			end = vo.getLimit();
		}
		pdto.setStart(start);
		pdto.setEnd(end);

		pdto.setDoCount(true);
		pdto.setQueryAll(false);
		pdto.setLimit(vo.getLimit());

		UserDetails user = (UserDetails) request.getSession().getAttribute("user");

		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("caseStatus", vo.getCaseStatus());
		map.put("queryAll", vo.getQueryAll());

		if (vo != null && StringUtils.isNotBlank(vo.getCaseCode()) && StringUtils.isNotEmpty(vo.getCaseCode())) {
			map.put("caseCode", "%" + vo.getCaseCode().trim() + "%");
		}

		if (vo != null && StringUtils.isNotBlank(vo.getKeyPoint()) && StringUtils.isNotEmpty(vo.getKeyPoint())) {
			map.put("keyPoint", "%" + vo.getKeyPoint().trim() + "%");
		}

		if (vo != null && StringUtils.isNotBlank(vo.getStartDate()) && StringUtils.isNotEmpty(vo.getStartDate())) {
			map.put("startDate", vo.getStartDate().trim());
		}

		if (vo != null && StringUtils.isNotBlank(vo.getEndDate()) && StringUtils.isNotEmpty(vo.getEndDate())) {
			map.put("endDate", vo.getEndDate().trim());
		}
		pdto.setQueryParams(map);

		Map<String, Object> result = auditFeedbackService.queryAuditFeedbackAll(pdto, user.getUserId());

		return result;
	}

	/**
	 * 医院反馈 确认
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("makeAuditFeedback.do")
	@ResponseBody
	public Map<String, Object> makeAuditFeedback(@RequestParam(value = "caseCode", defaultValue = "") String caseCode,
			HttpServletRequest request) throws AuditFeedbackException {
		Map<String, Object> result = new HashMap<String, Object>();

		String msg = "";
		if (StringUtils.isEmpty(caseCode) || StringUtils.isBlank(caseCode)) {
			msg = "案件编号不能为空";
		}
		if (StringUtils.isBlank(msg)) {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			try {
				auditFeedbackService.makeAuditFeedback(caseCode, user);
				result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
				result.put("msg", "操作成功");
			} catch (Exception e) {
				result.put(Constants.STATE_KEY, Constants.FAILED_STATE_VALUE);
				result.put("msg", e.getCause().getMessage());
			}

		} else {
			result.put(Constants.MSG_KEY, msg);
		}

		return result;
	}

	/**
	 * 医院反馈 申诉
	 * 
	 * @param vo
	 * @param request
	 * @return
	 */
	@RequestMapping("appealAuditFeedback.do")
	@ResponseBody
	public Map<String, Object> appealAuditFeedback(
			@RequestParam(value = "caseCode", defaultValue = "") String caseCode, HttpServletRequest request)
			throws AuditFeedbackException {
		Map<String, Object> result = new HashMap<String, Object>();

		String msg = "";
		if (StringUtils.isNotBlank(caseCode) && StringUtils.isNotBlank(caseCode)) {
			msg = "案件编号不能为空";
		}
		if (StringUtils.isNotBlank(msg)) {
			UserDetails user = (UserDetails) request.getSession().getAttribute("user");
			try {
				auditFeedbackService.appealAuditFeedback(caseCode, user);
				result.put(Constants.STATE_KEY, Constants.SUCCESS_STATE_VALUE);
				result.put("msg", "操作成功");
			} catch (Exception e) {
				result.put(Constants.STATE_KEY, Constants.FAILED_STATE_VALUE);
				result.put("msg", e.getMessage());
			}

		} else {
			result.put(Constants.MSG_KEY, msg);
		}
		return result;
	}

}
